package com.example.glgk.excel;

import lombok.extern.slf4j.Slf4j;

import java.text.*;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author ：Marvin Fu
 * @date ：Created in 2020/3/23 3:43 PM
 */
@Slf4j
public class NebulaDateFormat extends DateFormat
{
    private SimpleDateFormat outputDateFormat;

    private SimpleDateFormat inputDateFormat;

    private static final String DEFAULT_TIME_ZONE = "Asia/Shanghai";

    private static final String DEFAULT_INPUT_FORMAT = "yyyy-MM-dd";

//    private static final String DEFAULT_OUTPUT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DEFAULT_OUTPUT_FORMAT = "yyyy-MM-dd";

    public NebulaDateFormat() {
        inputDateFormat = new SimpleDateFormat(DEFAULT_INPUT_FORMAT);
        inputDateFormat.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
        outputDateFormat = new SimpleDateFormat(DEFAULT_OUTPUT_FORMAT);
        outputDateFormat.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
    }

    public NebulaDateFormat(String dateFormat) {
        inputDateFormat = new SimpleDateFormat(dateFormat);
        inputDateFormat.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
        outputDateFormat = new SimpleDateFormat(dateFormat);
        outputDateFormat.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
    }

    public NebulaDateFormat(String inputFormat, String outPutFormat) {
        inputDateFormat = new SimpleDateFormat(inputFormat);
        inputDateFormat.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
        outputDateFormat = new SimpleDateFormat(outPutFormat);
        outputDateFormat.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
    }

    public NebulaDateFormat(SimpleDateFormat inputFormat, SimpleDateFormat outPutFormat) {
        this.inputDateFormat = inputFormat;
        this.outputDateFormat = outPutFormat;
    }

    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        return outputDateFormat.format(date, toAppendTo, fieldPosition);
    }

    public String nebulaFormat(Date date) {
        return outputDateFormat.format(date);
    }

    @Override
    public Date parse(String source, ParsePosition pos) {
        Date date = inputDateFormat.parse(source, pos);
        return date;
    }

    @Override
    public Date parse(String source) throws ParseException
    {
        Date date = inputDateFormat.parse(source);
        return date;
    }
    @Override
    public Object clone() {
        return new NebulaDateFormat(this.inputDateFormat,this.outputDateFormat);
    }

}
